package cn.lystudio.rohm_time_op.controller.appApi;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.ObjectUtil;
import cn.lystudio.rohm_time_op.entity.dto.UserDepartmentDTO;
import cn.lystudio.rohm_time_op.entity.po.UsersPO;
import cn.lystudio.rohm_time_op.exceptions.JsonException;
import cn.lystudio.rohm_time_op.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/app-api/user")
@CrossOrigin
@SaIgnore
@Slf4j
public class AppUserController {
    private UserService userService;

    @Autowired
    @Lazy
    public void setUserService(UserService userService)
    {
        this.userService = userService;
    }

    @GetMapping("/getUserCodeEcho")
    public SaResult getUserCodeEcho(String userCode) {
        UserDepartmentDTO userDepartment = userService.get1UserDepartment_Usable(userCode);
        if(ObjectUtil.isEmpty(userDepartment)) throw new JsonException(500,"查无此人");
        return SaResult.data(userDepartment);
    }
    @PostMapping("/checkUserPassword")
    public SaResult checkUserPassword(@RequestBody UsersPO user){
        UsersPO userRes = userService.getUserLogin(user.getUserCode(), user.getUserPassword());
        if (ObjectUtil.isNotEmpty(userRes)) {
            return SaResult.ok();
        }
        return SaResult.error("密码错误");
    }

    @GetMapping("/getUserCodeEcho4UDBSU")
    public SaResult getUserCodeEcho4UDBSU(String userCode) {
        UserDepartmentDTO userDepartment = userService.get1UserDepartment_Usable(userCode);
        if(ObjectUtil.isEmpty(userDepartment)) throw new JsonException(500,"查无此人");
        if(ObjectUtil.isEmpty(userDepartment.getDepartmentsPO())) throw  new JsonException(500,"查无此人");
        if(userDepartment.getDepartmentsPO().getDepartmentID()!=146) throw  new JsonException(500,"查无此人");

        return SaResult.data(userDepartment);
    }

    @GetMapping("/getUserCodeEcho4UCD")
    public SaResult getUserCodeEcho4UCD(String userCode) {
        UserDepartmentDTO userDepartment = userService.get1UserDepartment_Usable(userCode);
        if(ObjectUtil.isEmpty(userDepartment)) throw new JsonException(500,"查无此人");
        if(ObjectUtil.isEmpty(userDepartment.getDepartmentsPO())) throw  new JsonException(500,"查无此人");
        if(userDepartment.getDepartmentsPO().getDepartmentID()!=147) throw  new JsonException(500,"查无此人");

        return SaResult.data(userDepartment);
    }

}
